package com.IR.product.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.IR.product.service.AProductBIZ;
import com.IR.product.service.BProductBIZ;
import com.IR.shop.po.Product;
/**
 * @author 单立鑫
 * @date 2018-10-01
 * */
@Controller
public class AProductAction {

	@Resource
	private AProductBIZ biz;
	@Resource
	private BProductBIZ biz2;
	/*删除商品*/
	@RequestMapping(value="deletePro.do")
	public void deletePro(Long proid,HttpServletResponse response) throws IOException {
		biz.deletePro(proid);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		out.println(1);
	}
	
	/*商品上架*/
	@RequestMapping(value="updateProUp.do")
	public void updateProUp(Long proid) {
		biz.updateProUp(proid);
	}
	
	/*商品下架*/
	@RequestMapping(value="updateProDown")
	public void updateProDown(Long proid) {
		biz.updateProDown(proid);
	}
	
	/*修改商品详细信息*/
	@RequestMapping(value="/updatePro.do")
	public void updatePro(Product pro,HttpServletResponse response,HttpServletRequest request) {
		//System.out.println("调用了");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");
		try {
			PrintWriter out=response.getWriter();
			String proname=request.getParameter("proname");
			String proprice=request.getParameter("proprice");
			String prooldprice=request.getParameter("prooldprice");
			String protext=request.getParameter("protext");
			String pronum=request.getParameter("pronum");
			String protypeid=request.getParameter("protypeid");
			String prosite=request.getParameter("prosite");
			pro=biz2.showProId(new Long(request.getParameter("proid")));
			pro.setProname(proname);
			pro.setProoldprice(Double.parseDouble(prooldprice));
			pro.setProprice(Double.parseDouble(proprice));
			pro.setProtext(protext);
			pro.setPronum(new Integer(pronum));
			pro.setProtypeid(new Integer(protypeid));
			pro.setProsite(prosite);
			pro.setIssale(1);
			biz.updatePro(pro);
			out.println(1);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/*添加商品以及商品的详细信息*/
	@RequestMapping(value="/savePro.do")
	public void savePro(Product pro,HttpServletResponse response,HttpServletRequest request) {
		try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setCharacterEncoding("utf-8");
			PrintWriter out=response.getWriter();
			String proname=request.getParameter("proname");
			String proprice=request.getParameter("proprice");
			String prooldprice=request.getParameter("prooldprice");
			String protext=request.getParameter("protext");
			String pronum=request.getParameter("pronum");
			String protypeid=request.getParameter("protypeid");
			String prosite=request.getParameter("prosite");
			pro.setProname(proname);
			pro.setProoldprice(Double.parseDouble(prooldprice));
			pro.setProprice(Double.parseDouble(proprice));
			pro.setProtext(protext);
			pro.setPronum(new Integer(pronum));
			pro.setProtypeid(new Integer(protypeid));
			pro.setProsite(prosite);
			pro.setIssale(1);
			biz.saveProAndDetail(pro);
			out.println(1);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
